package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.OrderForm;
import com.example.covid19.entity.OrderInfo;
import com.example.covid19.entity.User;
import com.example.covid19.service.OrderFormService;
import com.example.covid19.service.OrderInfoService;
import com.example.covid19.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-09-12
 */
@RestController
@RequestMapping("/order-info")
public class OrderInfoController {
    private final OrderInfoService orderInfoService;
    private final OrderFormService orderFormService;
    private final UserService userService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    public OrderInfoController(OrderInfoService orderInfoService, OrderFormService orderFormService, UserService userService) {
        this.orderInfoService = orderInfoService;
        this.orderFormService = orderFormService;
        this.userService = userService;
    }
    @RequestMapping("/userinfo")
    public Result userinfo(HttpServletRequest request){
        LocalDateTime dateTime1 = LocalDateTime.now();
        OrderForm orderForm = new OrderForm();
        String uuid = UUID.randomUUID().toString();
        String userid = request.getHeader("satoken");
        orderForm.setOrdernumber(uuid);
        stringRedisTemplate.opsForValue().set(userid,uuid);
        orderForm.setUsername(userService.getOne(new QueryWrapper<User>().eq(
            "userid",StpUtil.getLoginId()
        )).getUsername());
        orderForm.setOrderdate(dateTime1);
        System.out.println(StpUtil.getLoginId());
        orderForm.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
//        boolean flag = orderFormService.save(orderForm);
        boolean flag = true;
        if (flag){
            return Result.success(200,"信息填写成功",uuid);
        }
        else{
            return Result.fail(400,"信息填写错误",null);
        }

    }
    @RequestMapping("/gett")
    public Result gett(@RequestBody List<OrderInfo> jsonArray){
        orderInfoService.saveBatch(jsonArray);
      return Result.success(200,"ok",jsonArray);
    }
}
